/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.ejb;

import entities.Fantasyleague;
import entities.Fantasymatch;
import entities.Fantasyteam;
import entities.Week;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author The Lunatics
 */
@Stateless
public class FantasymatchFacade extends AbstractFacade<Fantasymatch> {

    @PersistenceContext(unitName = "Touchdown308PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FantasymatchFacade() {
        super(Fantasymatch.class);
    }

    public List<Fantasymatch> findMatchResultByLeagueId(Fantasyleague leagueId) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Query q = getEntityManager().createNamedQuery("Fantasymatch.findByleagueId");
        q.setParameter("leagueId", leagueId);
        return q.getResultList();
    }


    public List<Fantasymatch> findMatchResultByLeagueIdWeekId(Fantasyleague leagueId, Week weekId) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Query q = getEntityManager().createNamedQuery("Fantasymatch.findByleagueIdweekId");
        q.setParameter("leagueId", leagueId);
        q.setParameter("weekId", weekId);
        return q.getResultList();
    }

    public List<Fantasymatch> find(Fantasyleague league, Week week) {
        Query createQuery = getEntityManager().createQuery("SELECT f from Fantasymatch f WHERE f.leagueId = :leagueId AND f.weekId = :weekId");
        createQuery.setParameter("leagueId", league);
        createQuery.setParameter("weekId", week);

        return createQuery.getResultList();
    }
    
    public Fantasymatch find(Fantasyleague league, Week week, Fantasyteam homeTeam, Fantasyteam awayTeam) {
        Query createQuery = getEntityManager().createQuery("SELECT f from Fantasymatch f WHERE f.leagueId = :leagueId AND f.weekId = :weekId AND f.homeTeamId = :homeTeamId AND f.awayTeamId = :awayTeamId");
        createQuery.setParameter("leagueId", league);
        createQuery.setParameter("weekId", week);
        createQuery.setParameter("homeTeamId", homeTeam);
        createQuery.setParameter("awayTeamId", awayTeam);

        return (Fantasymatch) createQuery.getSingleResult();
    }
}
